//
//  CustomGuidView.m
//  baojia
//
//  Created by Zing on 9/13/14.
//  Copyright (c) 2014 com.baojia.www. All rights reserved.
//

#import "CustomGuidView.h"

@interface CustomGuidView()


@property(nonatomic , strong) UIScrollView * scrollView;
@end

@implementation CustomGuidView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        // Initialization code
    }
    return self;
}

- (void)addBlock:(void (^)(CustomGuidView * gView))block
{
    self.block = block;
}

- (void)initSubviewWithImages:(NSArray *)imgs
{
    if (!self.scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        [self.scrollView setPagingEnabled:YES];
    }
    [self addSubview:self.scrollView];
    int count = [imgs count];
    
    [self.scrollView setContentSize:CGSizeMake(SCREEN_WIDTH * count, 0)];
    
    for (int i = 0; i < count; i ++) {
        NSString * imgName = [imgs objectAtIndex:i];
        NSLog(@"imgName:%@",imgName);
        UIImage * image = [UIImage imageNamed:imgName];
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        imageView.image = image;
        [self.scrollView addSubview:imageView];
        if (i == count - 1) {
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer * taped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taped:)];
            [imageView addGestureRecognizer:taped];
        }
        
    }
}

- (void)taped:(UITapGestureRecognizer *)tap
{
    if(self.gType == GUIDE_TYPE_DEFAULT){
        if (self.block) {
            self.block(self);
            [self.scrollView removeFromSuperview];
            self.scrollView = nil;
            [self removeFromSuperview];
        }
    }else{
        [self.scrollView removeFromSuperview];
        self.scrollView = nil;
        [self removeFromSuperview];
    }
}

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
